package org.csu.molediary.api.controller.front;

import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Diary;
import org.csu.molediary.api.service.DiaryService;
import org.csu.molediary.api.vo.AccountId;
import org.csu.molediary.api.vo.DiaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/diaries/")
public class DiaryController {

    @Autowired
    private DiaryService diaryService;

    @GetMapping("allDiary")
    @ResponseBody
    //得到全部的日记
    public CommonResponse<List<DiaryVO>> getDiaryList(){
        return diaryService.getDiaryList();
    }

    @GetMapping("diaryListbycontent/{contentid}")
    @ResponseBody
    //根据日记id来得到某个日记
    public CommonResponse<DiaryVO> getDiaryByContentId(@PathVariable("contentid")int contentid){
        return diaryService.getDiaryByContentId(contentid);
    }

    @GetMapping("diaryListbyaccount/{accountid}")
    @ResponseBody
    //根据用户id来得到日记
    public CommonResponse<List<DiaryVO>> getDiaryByAccountId(@PathVariable("accountid")String accountid){
        return diaryService.getDiaryByAccountId(accountid);
    }

    @DeleteMapping("deleteDiary/{contentid}")
    @ResponseBody
    //删除某篇日记
    public CommonResponse<String> deleteDiaryByContentId(@PathVariable("contentid")int contentid){
        return diaryService.deleteDiaryByContentId(contentid);
    }

    @PostMapping("diary")
    @ResponseBody
    //新建日记
    public CommonResponse<Diary> createDiary(@RequestParam String content,@RequestParam String title,
                                             @RequestParam String accountid,@RequestParam int tagid,
                                             @RequestParam String province,@RequestParam String distinct,
                                             @RequestParam String city,@RequestParam String photo1,
                                             @RequestParam String photo2,@RequestParam String photo3,
                                             @RequestParam String diarydate){
        if(content == null){
            return CommonResponse.createForError("内容不能为空");
        }
        return diaryService.createDiary(content, title, accountid, tagid, province,
                                        distinct, city, photo1, photo2, photo3,diarydate);
    }

    @GetMapping("account/{contentid}")
    @ResponseBody
    //通过日记id获取用户id
    public CommonResponse<AccountId> getAccountIdByDiaryId(@PathVariable int contentid){
        return diaryService.getAccountIdByDiaryId(contentid);
    }

    @GetMapping("diary/{tit}")
    @ResponseBody
    //通过模糊查询标题来查询日记
    public CommonResponse<List<DiaryVO>> getDiaryByTitle(@PathVariable String tit){
        return diaryService.getDiaryByTitle(tit);
    }

    @GetMapping("diaryByGood/{accountid}")
    @ResponseBody
    //根据用户id获取用户点赞的所有日记信息
    public CommonResponse<List<DiaryVO>> getDiaryByGood(@PathVariable String accountid){
        return diaryService.getDiaryByGood(accountid);
    }
}
